package com.aifast.dict.autoconfigure;

import com.aifast.dict.cache.CacheService;
import com.aifast.dict.cache.ehcache.EhCacheUtil;
import com.aifast.dict.cache.redis.RedisUtil;
import com.aifast.dict.core.FastDictService;
import com.aifast.dict.core.FastDictTranslator;
import com.aifast.dict.properties.CaffeineSetProperties;
import com.aifast.dict.properties.EhCacheSetProperties;
import com.aifast.dict.properties.FastDictProperties;
import com.aifast.dict.cache.caffeine.CaffeineUtil;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.ehcache.Cache;
import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.ResourcePools;
import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.MemoryUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @Author AiFast
 * @since 2022/12/27 17:04
 */

@Import({FastDictTranslator.class,FastDictService.class, RedisUtil.class, CacheService.class})
@EnableConfigurationProperties(FastDictProperties.class)
@Configuration
public class FastDictAutoConfiguration extends CachingConfigurerSupport {

    @Autowired
    private FastDictProperties properties;

    @Bean
    @ConditionalOnProperty(prefix = "ai.dict",name = "mode",havingValue = "caffeine")
    public void FastDictCaffeineConfig(){
        CaffeineSetProperties set = properties.getCaffeine();
        /*初始化内存缓存*/
        CaffeineUtil.cache = CaffeineUtil.init(set.getInitialCapacity(), set.getMaximumSize(), set.getExpireSeconds());
        CaffeineUtil.cache.put("aifast","y78293gfuhjusy7812934jfcvsaoiro12309fjas");
    }

    @Bean
    @ConditionalOnProperty(prefix = "ai.dict",name = "mode",havingValue = "ehcache")
    public void FastDictEhCacheConfig(){
        EhCacheSetProperties ehcache = properties.getEhcache();
        /*初始化内存缓存*/
        EhCacheUtil.init(ehcache);
    }

    /**
     * 序列化配置
     * @return
     */
    private Jackson2JsonRedisSerializer getJackson2JsonRedisSerializer(){
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

        return jackson2JsonRedisSerializer;
    }

//    @Bean
//    @ConditionalOnProperty(prefix = "ai.dict",name = "mode",havingValue = "redis")
    public RedisTemplate<String, String> FastDictRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = getJackson2JsonRedisSerializer();

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

}
